package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TResponseBo;
import org.dromara.system.domain.vo.TResponseVo;
import org.dromara.system.domain.TResponse;
import org.dromara.system.mapper.TResponseMapper;
import org.dromara.system.service.ITResponseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 问卷收集Service业务层处理
 *
 * @author Lion Li
 * @date 2024-10-27
 */
@RequiredArgsConstructor
@Service
public class TResponseServiceImpl implements ITResponseService {

    private final TResponseMapper baseMapper;

    /**
     * 查询问卷收集
     *
     * @param responseId 主键
     * @return 问卷收集
     */
    @Override
    public TResponseVo queryById(Integer responseId){
        return baseMapper.selectVoById(responseId);
    }

    /**
     * 分页查询问卷收集列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 问卷收集分页列表
     */
    @Override
    public TableDataInfo<TResponseVo> queryPageList(TResponseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TResponse> lqw = buildQueryWrapper(bo);
        Page<TResponseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的问卷收集列表
     *
     * @param bo 查询条件
     * @return 问卷收集列表
     */
    @Override
    public List<TResponseVo> queryList(TResponseBo bo) {
        LambdaQueryWrapper<TResponse> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TResponse> buildQueryWrapper(TResponseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TResponse> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSurveyId() != null, TResponse::getSurveyId, bo.getSurveyId());
        lqw.eq(StringUtils.isNotBlank(bo.getUserId()), TResponse::getUserId, bo.getUserId());
        lqw.eq(bo.getResponseTime() != null, TResponse::getResponseTime, bo.getResponseTime());
        return lqw;
    }

    /**
     * 新增问卷收集
     *
     * @param bo 问卷收集
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TResponseBo bo) {
        TResponse add = MapstructUtils.convert(bo, TResponse.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setResponseId(add.getResponseId());
        }
        return flag;
    }

    /**
     * 修改问卷收集
     *
     * @param bo 问卷收集
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TResponseBo bo) {
        TResponse update = MapstructUtils.convert(bo, TResponse.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TResponse entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除问卷收集信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
